package com.mdb.open.trade.resource.trade;

/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfAttachFileService.java
* Author:   you.yichao
* Date:     2019-07-03
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-07-03          V1.00        代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.mdb.bc.api.data.Result;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.logger.LogAnnotation;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.bc.web.RepeatSubmitTypeEnum;
import com.mdb.bc.web.annotation.DenyRepeatSubmit;
import com.mdb.bc.web.support.SuperResources;
import com.mdb.open.common.enums.trade.status.TradeFileApplyStatusEnum;
import com.mdb.open.trade.domain.bo.trade.SaveAttachFileBo;
import com.mdb.open.trade.domain.dto.trade.SaveAttachFileDto;
import com.mdb.open.trade.domain.dto.trade.SaveTradeAttachFileDto;
import com.mdb.open.trade.domain.dto.trade.UpdateAttachFileDto;
import com.mdb.open.trade.domain.po.trade.TradeFileBasicInfo;
import com.mdb.open.trade.domain.vo.trade.AttachNoVo;
import com.mdb.open.trade.service.trade.TfAttachFileService;
import com.mdb.open.trade.service.trade.TfTradeFileBasicInfoService;
import com.mdb.open.trade.validator.CheckMultiStatusValidator;
import com.mdb.user.LoginUserInfoHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类描述：
 *  融资申请附件表 前端控制器
 *
 * @author you.yichao
 * @date 2019-07-03
 */
@RestController
@RequestMapping("tf/attach")
@Api(tags = "贸易金融融资申请附件")
public class AttachFileResource extends SuperResources {

    @Autowired
    TfAttachFileService tfAttachFileService;

    @Autowired
    LoginUserInfoHelper loginUserInfoHelper;

    @Autowired
    TfTradeFileBasicInfoService tfTradeFileBasicInfoService;

    @Autowired
    CheckMultiStatusValidator checkMultiStatusValidator;

    //-----------------------------------------------*  read  *----------------------------------------------------


    //-----------------------------------------------*  write  *----------------------------------------------------

    @LogAnnotation
    @ApiOperation("证明文件保存")
    @PostMapping("/explanation")
    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    public ResultEx<AttachNoVo> save(@RequestBody SaveAttachFileDto dto) {
        String customerCode = loginUserInfoHelper.getCustomerCode(true);

        if (!checkMultiStatusValidator.checkMerchantCustomer(customerCode, dto.getMerchantNo())) {
            return ResultEx.fail("商户信息不存在");
        }

        SaveAttachFileBo bo = new SaveAttachFileBo();
        bo.setApplyOrderNo(dto.getApplyOrderNo());
        bo.setBizOrderNo(dto.getBizOrderNo());
        bo.setType(dto.getType().getAttachFileTypeEnum());
        bo.setFileNo(dto.getFileNo());
        bo.setFileName(dto.getFileName());
        bo.setMerchantNo(dto.getMerchantNo());
        bo.setCustomerCode(customerCode);

        return tfAttachFileService.saveAttachFile(bo);
    }

    @LogAnnotation
    @ApiOperation("贸易档案附件保存")
    @PostMapping("/trade_file")
    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    public ResultEx<AttachNoVo> save(@RequestBody SaveTradeAttachFileDto dto) {

        String customerCode = loginUserInfoHelper.getCustomerCode(true);

        ResultEx<TradeFileBasicInfo> infoEx = tfTradeFileBasicInfoService.getInfoByNo(dto.getTradeFileNo());
        if (infoEx.isFailed() || ObjectUtil.isEmpty(infoEx.getData())) {
            return ResultEx.fail();
        }

        if (TradeFileApplyStatusEnum.ALREADY_APPLIED.equals(infoEx.getData().getApplyStatus())) {
            return ResultEx.fail("已提交申请，无法继续上传附件");
        }

        if (!checkMultiStatusValidator.checkMerchantCustomer(customerCode, infoEx.getData().getMerchantNo())) {
            return ResultEx.fail("商户信息不存在");
        }

        SaveAttachFileBo bo = new SaveAttachFileBo();
        bo.setApplyOrderNo(dto.getTradeFileNo());
        bo.setBizOrderNo(dto.getBizOrderNo());
        bo.setType(dto.getType().getAttachFileTypeEnum());
        bo.setFileNo(dto.getFileNo());
        bo.setFileName(dto.getFileName());
        bo.setMerchantNo(infoEx.getData().getMerchantNo());
        bo.setCustomerCode(customerCode);

        return tfAttachFileService.saveAttachFile(bo);
    }

    @LogAnnotation
    @ApiOperation("证明文件保存更新")
    @PostMapping("/update")
    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    public Result update(@RequestBody UpdateAttachFileDto attachFileDto) {
        return tfAttachFileService.updateAttachFile(attachFileDto);
    }

//    @LogAnnotation
//    @ApiOperation("证明文件保存 根据attachNo删除")
//    @PostMapping("/{attachNo}")
//    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
//    public Result remove(@ApiParam("附件编号") @PathVariable("attachNo") String attachNo) {
//        return tfAttachFileService.removeAttachFile(attachNo);
//    }

}
